cmake_minimum_required(VERSION 3.9.0)
project(build-deps NONE)
cmake_host_system_information(
  RESULT build_concurrency_factor
  QUERY NUMBER_OF_LOGICAL_CORES)
set(info_dir --infodir=@REDPANDA_DEPS_INSTALL_DIR@/share/info)
include(ExternalProject)

set(c_flags "@BASE_C_FLAGS@ @CMAKE_C_FLAGS_BUILD_TYPE@")
set(cxx_flags "@BASE_CXX_FLAGS@ @CMAKE_CXX_FLAGS_BUILD_TYPE@ -std=c++20")
set(ld_flags "@BASE_LD_FLAGS@")
set(default_depends "")

set(common_cmake_args
  -DCMAKE_CXX_STANDARD=@CMAKE_CXX_STANDARD@
  -DCMAKE_CXX_EXTENSIONS=@CMAKE_CXX_EXTENSIONS@
  -DCMAKE_C_COMPILER=@CMAKE_C_COMPILER@
  -DCMAKE_CXX_COMPILER=@CMAKE_CXX_COMPILER@
  -DCMAKE_C_COMPILER_LAUNCHER=@CMAKE_C_COMPILER_LAUNCHER@
  -DCMAKE_CXX_COMPILER_LAUNCHER=@CMAKE_CXX_COMPILER_LAUNCHER@
  "-DCMAKE_C_FLAGS=@BASE_C_FLAGS@"
  "-DCMAKE_C_FLAGS_@BUILD_TYPE@=@CMAKE_C_FLAGS_BUILD_TYPE@"
  "-DCMAKE_CXX_FLAGS=@BASE_CXX_FLAGS@ -std=c++20"
  "-DCMAKE_CXX_FLAGS_@BUILD_TYPE@=@CMAKE_CXX_FLAGS_BUILD_TYPE@"
  "-DCMAKE_EXE_LINKER_FLAGS=@CMAKE_EXE_LINKER_FLAGS@"
  "-DCMAKE_SHARED_LINKER_FLAGS=@CMAKE_SHARED_LINKER_FLAGS@"
  "-DCMAKE_MODULE_LINKER_FLAGS=@CMAKE_MODULE_LINKER_FLAGS@"
  -DCMAKE_BUILD_TYPE=@CMAKE_BUILD_TYPE@
  -DCMAKE_PREFIX_PATH=@REDPANDA_DEPS_INSTALL_DIR@
  -DCMAKE_INSTALL_PREFIX=@REDPANDA_DEPS_INSTALL_DIR@
  -DCMAKE_INSTALL_RPATH=@REDPANDA_DEPS_INSTALL_DIR@/lib
  -DCMAKE_SKIP_BUILD_RPATH=OFF
  -DCMAKE_BUILD_WITH_INSTALL_RPATH=ON
  -DCMAKE_POSITION_INDEPENDENT_CODE=@CMAKE_POSITION_INDEPENDENT_CODE@
  -DCMAKE_VERBOSE_MAKEFILE=@CMAKE_VERBOSE_MAKEFILE@
)
# pkgconfig
set(ENV{PKG_CONFIG_PATH}  @PKG_CONFIG_PATH@)
set(cmake_build_env)
if (@CMAKE_CXX_COMPILER_ID@ MATCHES "GNU")
  set(seastar_ld_flags "${ld_flags} -pthread")
  set(seastar_cxx_flags "-Wno-error$<SEMICOLON>-fcoroutines")
endif()

if ("@CMAKE_CXX_COMPILER_ID@" MATCHES "Clang")
  set(AR_EXE llvm-ar)
else()
  set(AR_EXE ar)
endif()

# Find ar binary
execute_process(
  COMMAND
    @CMAKE_C_COMPILER@ @BASE_C_FLAGS@ -print-prog-name=${AR_EXE}
  OUTPUT_VARIABLE
    AR_PROGRAM
  OUTPUT_STRIP_TRAILING_WHITESPACE
)

# Get absolute path to binary
execute_process(
  COMMAND
    bash -c "type -p ${AR_PROGRAM}"
  OUTPUT_VARIABLE
    AR_PROGRAM
  OUTPUT_STRIP_TRAILING_WHITESPACE
)

# Cache AR
set(AR ${AR_PROGRAM} CACHE FILEPATH "AR")

# autotools
set(base_env
  CC=@CMAKE_C_COMPILER@
  CXX=@CMAKE_CXX_COMPILER@
  AR=${AR}
  PKG_CONFIG_PATH=@PKG_CONFIG_PATH@
)
set(configure_env
  ${base_env}
  CFLAGS=${c_flags}
  CXXFLAGS=${cxx_flags}
  LDFLAGS=${ld_flags}
)
# For a libtool project
set(build_env
  ${base_env}
  CFLAGS=${c_flags}
  CXXFLAGS=${cxx_flags}
  "LDFLAGS=${ld_flags}"
)

# Don't include -std=c++20 for v8 build
set(cxx_flags_v8 "@BASE_CXX_FLAGS@ @CMAKE_CXX_FLAGS_BUILD_TYPE@")
set(configure_env_v8
  ${base_env}
  CFLAGS=${c_flags}
  CXXFLAGS=${cxx_flags_v8}
  LDFLAGS=${ld_flags}
)

if ("@CMAKE_CXX_COMPILER_ID@" MATCHES "Clang")
    set(toolset clang)
    set(boost_user_config_jam "using clang : : @CMAKE_CXX_COMPILER@ $<SEMICOLON>")
elseif (@CMAKE_CXX_COMPILER_ID@ STREQUAL "GNU")
    set(toolset gcc)
    set(boost_user_config_jam "using gcc : : @CMAKE_CXX_COMPILER@ : $<SEMICOLON>")
endif()

# define make command in terms of cmake
set(make_command make ${build_env} -j${build_concurrency_factor} )
if ("@CMAKE_BUILD_TYPE@" MATCHES "Debug")
  set(BOOST_BUILD_VARIANT debug)
else ()
  set(BOOST_BUILD_VARIANT release)
endif()
ExternalProject_Add(boost
  # Boost outcome is only installed on boost 1.70.0+
  URL https://storage.googleapis.com/vectorizedio-public/dependencies/boost_1_75_0.tar.gz
  URL_MD5 38813f6feb40387dfe90160debd71251
  INSTALL_DIR    @REDPANDA_DEPS_INSTALL_DIR@
  PATCH_COMMAND
    ./bootstrap.sh
    --prefix=@REDPANDA_DEPS_INSTALL_DIR@
    --with-libraries=atomic,chrono,date_time,filesystem,program_options,system,test,thread
    &&
    echo
    "${boost_user_config_jam}"
    > <SOURCE_DIR>/tools/build/src/user-config.jam
  CONFIGURE_COMMAND ""
  BUILD_COMMAND ""
  DEPENDS ${default_depends}
  INSTALL_COMMAND
    ${CMAKE_COMMAND} -E chdir <SOURCE_DIR>
    ./b2
    -j ${build_concurrency_factor}
    --toolset=${toolset}
    --layout=system
    --build-dir=<BINARY_DIR>
    install
    variant=${BOOST_BUILD_VARIANT}
    cflags=${c_flags}
    cxxflags=${cxx_flags}
    linkflags=${ld_flags}
    link=shared
    threading=multi
    hardcode-dll-paths=true
    dll-path=@REDPANDA_DEPS_INSTALL_DIR@/lib)

ExternalProject_Add(seastar
  GIT_REPOSITORY https://github.com/vectorizedio/seastar.git
  GIT_TAG d7fa70d42dfc5665b7914768e0accaffc365a8ed
  INSTALL_DIR    @REDPANDA_DEPS_INSTALL_DIR@
  CMAKE_COMMAND ${CMAKE_COMMAND} -E env ${cmake_build_env} ${CMAKE_COMMAND}
  CMAKE_ARGS
    ${common_cmake_args}
    -DBoost_USE_STATIC_LIBS=OFF
    -DBoost_NO_BOOST_CMAKE=ON
    -DBoost_NO_SYSTEM_PATHS=TRUE
    -DSeastar_INSTALL=ON
    -DSeastar_DPDK=OFF
    -DSeastar_APPS=OFF
    -DSeastar_DEMOS=OFF
    -DSeastar_DOCS=OFF
    -DSeastar_TESTING=OFF
    -DSeastar_CXX_FLAGS=${seastar_cxx_flags}
    "-DSeastar_LD_FLAGS=${seastar_ld_flags}"
    -DSeastar_API_LEVEL=6
    -DSeastar_CXX_DIALECT=c++20
    -DSeastar_UNUSED_RESULT_ERROR=ON
  INSTALL_COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --target install
  DEPENDS ${default_depends} boost)

ExternalProject_Add(HdrHistogram
  URL https://storage.googleapis.com/vectorizedio-public/dependencies/HdrHistogram_c-0.11.2.tar.gz
  URL_MD5 95970dea64f1a7a8d199aeb9f7c15e60
  INSTALL_DIR    @REDPANDA_DEPS_INSTALL_DIR@
  CMAKE_COMMAND ${CMAKE_COMMAND} -E env ${cmake_build_env} ${CMAKE_COMMAND}
  CMAKE_ARGS
    ${common_cmake_args}
    -DHDR_HISTOGRAM_BUILD_PROGRAMS=OFF
    -DHDR_HISTOGRAM_BUILD_SHARED=OFF
  DEPENDS ${default_depends})

ExternalProject_Add(abseil
  GIT_REPOSITORY https://github.com/vectorizedio/abseil-cpp.git
  GIT_TAG e1d388e7e74803050423d035e4374131b9b57919
  INSTALL_DIR    @REDPANDA_DEPS_INSTALL_DIR@
  CMAKE_COMMAND ${CMAKE_COMMAND} -E env ${cmake_build_env} ${CMAKE_COMMAND}
  DEPENDS ${default_depends}
  CMAKE_ARGS
    ${common_cmake_args}
    -DABSL_ENABLE_INSTALL=ON)

ExternalProject_Add(crc32c
  GIT_REPOSITORY https://github.com/vectorizedio/crc32c.git
  GIT_TAG 47b40d220949c5b013d49c1179643334196ec5a9
  INSTALL_DIR    @REDPANDA_DEPS_INSTALL_DIR@
  CMAKE_COMMAND ${CMAKE_COMMAND} -E env ${cmake_build_env} ${CMAKE_COMMAND}
  DEPENDS ${default_depends}
  CMAKE_ARGS
    ${common_cmake_args}
    -DCRC32C_BUILD_TESTS=0
    -DCRC32C_BUILD_BENCHMARKS=0
    -DCRC32C_USE_GLOG=0)

ExternalProject_Add(roaring
  GIT_REPOSITORY https://github.com/vectorizedio/CRoaring.git
  GIT_TAG 7b9c83740d4f764941d38144cc8e2122076e38e4
  INSTALL_DIR    @REDPANDA_DEPS_INSTALL_DIR@
  DEPENDS ${default_depends}
  CMAKE_COMMAND ${CMAKE_COMMAND} -E env ${cmake_build_env} ${CMAKE_COMMAND}
  CMAKE_ARGS
  ${common_cmake_args}
  -DENABLE_ROARING_TESTS=OFF
  -DROARING_BUILD_STATIC=ON)

ExternalProject_Add(alien_thread
  GIT_REPOSITORY https://github.com/vectorizedio/alien_thread.git
  GIT_TAG bdfb56f
  INSTALL_DIR @REDPANDA_DEPS_INSTALL_DIR@
  DEPENDS ${default_depends}
  PATCH_COMMAND ""
  CONFIGURE_COMMAND ""
  BUILD_COMMAND ""
  INSTALL_COMMAND
  COMMAND ${CMAKE_COMMAND} -E copy_directory <SOURCE_DIR>/include/v <INSTALL_DIR>/include/v)

ExternalProject_Add(
  librdkafka
  GIT_REPOSITORY https://github.com/edenhill/librdkafka
  GIT_TAG v1.2.2
  INSTALL_DIR @REDPANDA_DEPS_INSTALL_DIR@
  DEPENDS ${default_depends} boost
  CMAKE_COMMAND ${CMAKE_COMMAND} -E env ${cmake_build_env} ${CMAKE_COMMAND}
  CMAKE_ARGS
    ${common_cmake_args}
    -DWITH_ZLIB=OFF
    -DWITH_ZSTD=OFF
    -DENABLE_LZ4_EXT=OFF
    -DRDKAFKA_BUILD_STATIC=ON
    -DRDKAFKA_BUILD_EXAMPLES=OFF
    -DRDKAFKA_BUILD_TESTS=OFF)

ExternalProject_Add(cppkafka
  GIT_REPOSITORY https://github.com/mfontanini/cppkafka.git
  GIT_TAG c3b4580
  INSTALL_DIR @REDPANDA_DEPS_INSTALL_DIR@
  DEPENDS librdkafka
  CMAKE_COMMAND ${CMAKE_COMMAND} -E env ${cmake_build_env} ${CMAKE_COMMAND}
  CMAKE_ARGS
    ${common_cmake_args}
    -DCPPKAFKA_BUILD_SHARED=OFF
    -DCPPKAFKA_RDKAFKA_STATIC_LIB=ON
    -DCPPKAFKA_BOOST_STATIC_LIBS=OFF
    -DCPPKAFKA_DISABLE_EXAMPLES=OFF
    -DCPPKAFKA_DISABLE_TESTS=OFF)

ExternalProject_Add(base64_ext
  GIT_REPOSITORY https://github.com/vectorizedio/base64
  GIT_TAG eaebee8
  INSTALL_DIR @REDPANDA_DEPS_INSTALL_DIR@
  DEPENDS ${default_depends}
  CONFIGURE_COMMAND ""
  BUILD_COMMAND
    ${CMAKE_COMMAND} -E env ${build_env}
    make ${build_env} lib/libbase64.o -j${build_concurrency_factor}
  BUILD_IN_SOURCE true
  INSTALL_COMMAND
    COMMAND ${CMAKE_COMMAND} -E copy <SOURCE_DIR>/include/libbase64.h <INSTALL_DIR>/include/libbase64.h
    COMMAND ${CMAKE_COMMAND} -E copy <SOURCE_DIR>/lib/libbase64.o <INSTALL_DIR>/lib/libbase64.o)

ExternalProject_Add(kafka-codegen-pex
  DOWNLOAD_COMMAND ""
  INSTALL_DIR @REDPANDA_DEPS_INSTALL_DIR@
  PATCH_COMMAND ""
  CONFIGURE_COMMAND ""
  BUILD_COMMAND ""
  INSTALL_COMMAND
  COMMAND ${CMAKE_COMMAND} -E chdir <SOURCE_DIR> python3 -m venv env
  COMMAND <SOURCE_DIR>/env/bin/pip install pex
  COMMAND <SOURCE_DIR>/env/bin/pex jsonschema jinja2 -o <INSTALL_DIR>/bin/kafka-codegen-venv)

ExternalProject_Add(kafka-python-pex
  DOWNLOAD_COMMAND ""
  INSTALL_DIR @REDPANDA_DEPS_INSTALL_DIR@
  PATCH_COMMAND ""
  CONFIGURE_COMMAND ""
  BUILD_COMMAND ""
  INSTALL_COMMAND
  COMMAND ${CMAKE_COMMAND} -E chdir <SOURCE_DIR> python3 -m venv env
  COMMAND <SOURCE_DIR>/env/bin/pip install pex
  COMMAND <SOURCE_DIR>/env/bin/pex kafka-python -o <INSTALL_DIR>/bin/kafka-python-env
  )

ExternalProject_Add(ctre
  GIT_REPOSITORY https://github.com/hanickadot/compile-time-regular-expressions
  GIT_TAG v3.3.4
  INSTALL_DIR @REDPANDA_DEPS_INSTALL_DIR@
  CMAKE_COMMAND ${CMAKE_COMMAND} -E env ${cmake_build_env} ${CMAKE_COMMAND}
  CMAKE_ARGS
    ${common_cmake_args}
    -DCTRE_BUILD_TESTS=OFF
    -DCTRE_BUILD_PACKAGE=OFF
    -DCTRE_BUILD_PACKAGE_DEB=OFF
    -DCTRE_BUILD_PACKAGE_RPM=OFF
)

if ("@CMAKE_CXX_COMPILER_ID@" MATCHES "Clang")
  set(is_clang true)
else()
  set(is_clang false)
endif()

if (@CMAKE_BUILD_TYPE@ MATCHES "Debug")
  set(v8_debug true)
else()
  set(v8_debug false)
endif()

set(v8_flags
  "--args=v8_monolithic=true\
  is_debug=${v8_debug}\
  is_clang=${is_clang}\
  use_gold=false\
  clang_use_chrome_plugins=false\
  treat_warnings_as_errcors=false\
  use_custom_libcxx=false\
  use_glib=false\
  use_sysroot=false\
  v8_use_external_startup_data=false\
  is_component_build=false\
  v8_enable_pointer_compression=false\
  custom_toolchain=\"//build/toolchain/linux/unbundle:default\"\
  host_toolchain=\"//build/toolchain/linux/unbundle:default\"")
ExternalProject_Add(v8
INSTALL_DIR @REDPANDA_DEPS_INSTALL_DIR@
DOWNLOAD_COMMAND
  COMMAND @DEPOT_TOOLS_DIR@/gclient configure https://github.com/v8/v8.git
  COMMAND @DEPOT_TOOLS_DIR@/gclient sync -r e04bb9be8542b166c4dda1a77bfb1c46552afdd8
PATCH_COMMAND ""
CONFIGURE_COMMAND
  COMMAND cd <SOURCE_DIR> # Is used for run gn inside v8 dir
  COMMAND ${CMAKE_COMMAND} -E copy @DEPOT_TOOLS_DIR@/installed-gn/gn <SOURCE_DIR>/buildtools/linux64/gn
  COMMAND ${CMAKE_COMMAND} -E env ${configure_env_v8}
   @DEPOT_TOOLS_DIR@/gn gen <BINARY_DIR> --root=<SOURCE_DIR> ${v8_flags}
  COMMAND @DEPOT_TOOLS_DIR@/gn desc <BINARY_DIR> --root=<SOURCE_DIR> //:v8_monolith --tree
BUILD_COMMAND
  COMMAND ${CMAKE_COMMAND} -E env ${configure_env_v8}
   ninja -C <BINARY_DIR> v8_monolith
INSTALL_COMMAND
  COMMAND ${CMAKE_COMMAND} -E copy <BINARY_DIR>/obj/libv8_monolith.a <INSTALL_DIR>/lib/libv8_monolith.a
  COMMAND ${CMAKE_COMMAND} -E copy_directory <SOURCE_DIR>/include <INSTALL_DIR>/include
)
